/**
 * @(#) PacdpProxy.as
 *
 * @date		2012. 09. 19.
 * @version		1.0
 * @author		Park, Jutae
 * @description	 CDP신청 Proxy - Proxy
 * 				CDP 신청 리스트를 조회하고 저장, 수정, 삭제한다.
 */
package com.skt.common_hr.inrec.model
{
	import com.skt.common_hr.common.Variables;
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.inrec.InrecFacade;
	import com.skt.common_hr.inrec.model.vo.InrecSaveVO;
	import com.skt.common_hr.inrec.model.vo.InrecVO;
	import com.wcl.dataloader.*;
	import com.wcl.utils.DateUtil;
	import com.wcl.utils.StringUtil;
	
	import mx.collections.ArrayCollection;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
		
	public class InrecProxy extends CommonProxy
	{
		public static const NAME:String = "InrecProxy";
		public static const LOAD_STATEMENT_URL_COMPLTE:String		= "loadStatementUrlComplete";
		public static const LOAD_STATEMENT_URL_FAILED:String		= "loadStatementUrlFailed";
		
		public var inrecList:ArrayCollection;
		public var sinChungList:ArrayCollection;
		public var acMedYear:ArrayCollection;
		public var sRettext:String;
		public var readList:ArrayCollection;
		public var causeList:ArrayCollection;
		public var readList2:ArrayCollection;
		public var errCode:int;
		public var errText:String;
		public var url:String;
		
		public function InrecProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		public function loadYear(pernr:String, year:String=''):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_GJAHR"] = year;
			
			loadData("Z_HR_IN01", params, loadYearResult, loadFault); 
		}
		
		public function load(pernr:String, year:String=''):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_GJAHR"] = year;
			
			loadData("Z_HR_IN01", params, loadResult, loadFault); 
		}
		
		public function insert(pernr:String, rec:InrecVO, table1:Array):void
		{
			
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "INS"; 			//INS-신규, MOD-수정
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["ITAB"] = table;
			tables["ITABT"] = table1;

			saveData("Z_HR_IN03", params, tables, insertResult, insertFault); 
		}
		
		public function readLoad(pernr:String, _obj:InrecVO):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_GJAHR"] = _obj.GJAHR;
			
			loadData("Z_HR_IN01", params, loadResult1, loadFault); 
		}

		public function del(pernr:String, _obj:InrecVO):void
		{ 
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_BEGDA"] = DateUtil.toBasicString(_obj.BEGDA);
			params["I_ENDDA"] = DateUtil.toBasicString(_obj.ENDDA);
			params["I_SEQNR"] = _obj.SEQNR;

			loadData("Z_HR_IN02", params, delResult, delFault); 
		}
		
		public function update(pernr:String, rec:InrecVO, table2:Array):void
		{
			var params:Object = new Object();
			params["I_PERNR"] = pernr;
			params["I_ACTIO"] = "MOD"; 		//INS-신규, MOD-수정	
			params["I_BEGDA"] = DateUtil.toBasicString(rec.BEGDA);
			params["I_ENDDA"] = DateUtil.toBasicString(rec.ENDDA);
			params["I_SEQNR"] = rec.SEQNR;
			 
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(rec);
			tables["ITAB"] = table;
			tables["ITABT"] = table2;
			
			saveData("Z_HR_IN03", params, tables, updateResult, updateFault); 
		}
		
		public function loadAttach(pernr:String, _obj:InrecVO):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = Variables.myPernr;
			param["I_BEGDA"] = DateUtil.toBasicString(_obj.BEGDA);
			param["I_ENDDA"] = DateUtil.toBasicString(_obj.ENDDA);
			param["I_SEQNR"] = _obj.SEQNR;
			
			loadData("Z_HR_IN04", param, resultAttach, fault); 
		}
		
		public function loadYearResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				acMedYear = parseYear(event.result.T_GJAHR);
				facade.sendNotification(InrecFacade.LOAD_YEAR_COMPLETE, this);
			}
			else {
				facade.sendNotification(InrecFacade.LOAD_YEAR_FAILED, this);
			}
		}

		public function loadResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				inrecList = parseInrecList(event.result.OTAB);
				sinChungList = parseSinchungList(event.result.OTAB2);
				causeList = parseCause(event.result.OTABT)
				facade.sendNotification(InrecFacade.LOAD_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(InrecFacade.LOAD_LIST_FAILED, this);
			}
		}
		
		public function loadResult1(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  	
				readList 		= parseList(event.result.OTAB);
				readList2 		= parseList2(event.result.OTABT);
				facade.sendNotification(InrecFacade.READ_LIST_COMPLETE, this);
			}
			else {
				facade.sendNotification(InrecFacade.READ_LIST_FAILED, this);
			}
		}
		
		public function resultAttach(event:ResultEvent):void
		{
			errCode = event.result["RETCODE"];
			errText = event.result["RETTEXT"];
			if(event.result["RETCODE"]==0) {
				url = StringUtil.NVL(event.result["O_URL"]);
				facade.sendNotification(InrecProxy.LOAD_STATEMENT_URL_COMPLTE, this);
			}
			else {
				trace( "failed to 정산전표 URL");
				url = "";
				facade.sendNotification(InrecProxy.LOAD_STATEMENT_URL_FAILED, this);
			}
		}

		private function fault(event:FaultEvent):void
		{
			trace(event.fault.faultString);
			errCode = -1;
			errText = "호출실패";
			url = "";
			facade.sendNotification(InrecProxy.LOAD_STATEMENT_URL_FAILED, this);
		}
		
		private function loadFault(event:FaultEvent):void
		{
			facade.sendNotification(InrecFacade.LOAD_LIST_FAILED, this);
		}
		
		public var failedAlert:String; 
		public function insertResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(InrecFacade.INSERT_COMPLETE, this);
			}
			else {
				failedAlert = event.result.RETTEXT;
				facade.sendNotification(InrecFacade.CANCEL_REQUEST_INPUT, this);
			}
		}
		
		private function insertFault(event:FaultEvent):void
		{
			facade.sendNotification(InrecFacade.INSERT_FAILED, this);
		}
		
		public var updateFailed:String; 
		public function updateResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {   
				facade.sendNotification(InrecFacade.OK_REQUEST_INPUT, this);
			}
			else {
				updateFailed = event.result.RETTEXT; 
				facade.sendNotification(InrecFacade.CANCEL_REQUEST_INPUT, this);
			}
		}

		private function updateFault(event:FaultEvent):void
		{
			facade.sendNotification(InrecFacade.UPDATE_FAILED, this);
		}
		
		public function delResult(event:ResultEvent):void
		{
			if(event.result["RETCODE"]==0) {  
				facade.sendNotification(InrecFacade.DELETE_COMPLETE, this);
			}
			else {
				facade.sendNotification(InrecFacade.DELETE_FAILED, this);
			}
		}
		
		private function delFault(event:FaultEvent):void
		{
			facade.sendNotification(InrecFacade.DELETE_FAILED, this);
		}
		
		private function parseInrecList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:InrecVO = new InrecVO();
				vo.APPBR		= rec["APPBR"];
				vo.APPNM		= rec["APPNM"];
				vo.APPOR 		= rec["APPOR"];
				vo.APPRE		= rec["APPRE"];
				vo.APPRET 		= rec["APPRET"];
				vo.APPST		= rec["APPST"];
				vo.APPSTT		= rec["APPSTT"];
				vo.APPTE	 	= rec["APPTE"];
				vo.BEGDA		= DateUtil.stringToDateTime(rec["BEGDA"]);
				vo.ENDDA		= DateUtil.stringToDateTime(rec["ENDDA"]);
				vo.PERNR	 	= rec["PERNR"];
				vo.SEQNR		= rec["SEQNR"];
				vo.SRTFD		= rec["SRTFD"];
				
				// 변경삭제 유무 
				if(vo.APPST == '1'){
					vo.APPST_F = true;
				} else {
					vo.APPST_F = false;
				}	
				
				retArr.addItem(vo);
			}
			return retArr;
		}
		
		private function parseCause(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:InrecSaveVO = new InrecSaveVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "BEGDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
		
		private function parseSinchungList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:InrecVO = new InrecVO();
				vo.ORGTX 		= rec["ORGTX"];
				vo.PERNR		= rec["PERNR"];
				vo.ENAME		= rec["ENAME"];
				vo.PTEXT 		= rec["PTEXT"];
				vo.INTEL		= rec["INTEL"];
				
				retArr.addItem(vo);
			}
			return retArr;	
		}
		
		private function parseYear(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr)
			{
				retArr.addItem({label:rec["TEXT"], data:rec["CODE"]});
			}
			return retArr;
		}
		
		private function parseList(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:InrecVO = new InrecVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "BEGDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
		
		private function parseList2(recArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();


			for each(var obj:Object in recArr){
				var retObj:InrecSaveVO = new InrecSaveVO();
				for(var field:String in obj) {
				     if(retObj.hasOwnProperty(field)==false ) {
				      continue;
				     }
				     
					if(field == "BEGDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else if(field == "ENDDA") {
						retObj[field] = DateUtil.stringToDateTime(obj[field]);
					}
					else {
						retObj[field] = obj[field];
					}
				}
				retArr.addItem(retObj);
			}
			return retArr;
		}
				
	}
}